package mrms.api.web.controllers;

import lombok.AllArgsConstructor;
import mrms.api.entity.RoomEntity;
import mrms.api.repository.RoomRepository;
import mrms.api.web.utils.Result;
import mrms.api.web.utils.RoomType;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

/**
 * @author lenchu
 * @date 2019/02/05
 */
@RestController
@AllArgsConstructor
public class RoomController {
    private final RoomRepository roomRepository;

    @GetMapping("/room/{id}")
    public Result findById(@PathVariable String id) {
        return Result.success(roomRepository.findById(id).get());
    }

    @GetMapping("/room")
    public Result findAll() {
        return Result.success(roomRepository.findAllByTypeGreaterThanEqual(RoomType.REAL));
    }

    @GetMapping("/room/size/{size}")
    public Result findBySize(@PathVariable Integer size, @PageableDefault(sort = "size") Pageable pageable) {
        return Result.success(roomRepository.findAllBySizeGreaterThanEqual(size, pageable));
    }

    @PostMapping("/a/room")
    public Result save(@RequestBody RoomEntity room) {
        room.setType(room.getType());
        return Result.success(roomRepository.save(room));
    }

    @DeleteMapping("/a/room/{id}")
    public Result deleteById(@PathVariable String id) {
        RoomEntity roomEntity = roomRepository.findById(id).get();
        roomRepository.deleteById(id);
        return Result.success(roomEntity);
    }


}
